package com.toutou.base.bindhelper

import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import java.util.regex.Pattern


class TextBDAdapter {
    companion object {

        @JvmStatic
        @BindingAdapter("tColor")
        fun setTextColor(view: TextView, tColor: Int) {
            view.setTextColor(tColor)
        }

        /**
         * 高亮搜索文字
         */
        @JvmStatic
        @BindingAdapter(
            value = ["withHighLightText", "highlight"],
            requireAll = false
        )
        fun setHighLight(
            view: TextView,
            withHighLightText: String,
            highlight: String,
        ) {
            view.text = withHighLightText
            //高亮标识
            highlightText(view, highlight)

        }

        private fun highlightText(view: TextView, keywords: String){

            view.text = highlightKeyword(view.text.toString(), keywords)
            view.invalidate()
            return

//           以下为分词匹配，目前用不到

//            val keywordArr = keywords.trim().toCharArray()
//
////            把keywordArray用|拼接成字符串
//            var endKeyWords = ""
//            for (i in keywordArr.indices) {
//                endKeyWords += keywordArr[i]
//                if (i < keywordArr.size - 1) {
//                    endKeyWords += ","
//                }
//            }
//
//            // 创建一个SpannableString
//            val spannableString = SpannableString(view.text)
//            // 创建正则表达式模式,并忽略大小写
//            val pattern = Pattern.compile("[${endKeyWords}]", Pattern.CASE_INSENSITIVE)
//            val matcher = pattern.matcher(spannableString)
//
//
//            // 设置高亮颜色
//            val highlightColor =
//                ContextCompat.getColor(view.context, com.qianyitms.qtms.resource.R.color.cl_295DF0)
//            var lastEnd = 0
//
//            // 查找并高亮匹配的词汇
//            while (matcher.find()) {
//                val startIndex = matcher.start()
//                val endIndex = matcher.end()
//
//                // 设置高亮颜色
//                val foregroundColorSpan = ForegroundColorSpan(highlightColor)
//                spannableString.setSpan(
//                    foregroundColorSpan,
//                    startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
//                )
//
//                // 更新最后一个匹配的结束位置
//                lastEnd = endIndex
//            }
//
//            // 将带有高亮的SpannableString设置到TextView
//            view.text = spannableString
//            view.invalidate()
//            return containsAllCharacters(view.text.toString(), keywords)
        }

        private fun containsAllCharacters(source: String, target: String): Boolean {
            val targetChars: MutableSet<Char> = HashSet()
            for (c in target.uppercase().toCharArray()) {
                targetChars.add(c)
            }

            for (c in source.uppercase().toCharArray()) {
                targetChars.remove(c)
                if (targetChars.isEmpty()) {
                    return true
                }
            }

            return targetChars.isEmpty()
        }

        private fun highlightKeyword(text: String, keyword: String): SpannableString {
            val spannableString = SpannableString(text)
            val keywordPattern = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE)
            val matcher = keywordPattern.matcher(spannableString)

            while (matcher.find()) {
                val start = matcher.start()
                val end = matcher.end()
                spannableString.setSpan(
                    ForegroundColorSpan(Color.parseColor("#295DF0")),
                    start,
                    end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
            }

            return spannableString
        }

    }

}